package se.statifyme.client.forms;

import se.statifyme.client.record.RecordClient;
import se.statifyme.client.record.RecordClient.ContentType;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class TransferSettingsForm extends Composite {

	interface Binder extends UiBinder<Widget, TransferSettingsForm> {}
	
	@UiField
	ListBox contentTypesField;
	
	public TransferSettingsForm() {
		initWidget(GWT.<Binder>create(Binder.class).createAndBindUi(this));
		
		ContentType current = RecordClient.INSTANCE.getContentType();
		for (ContentType contentType : ContentType.values()) {
			String item = contentType.name();
			String value = contentType.getContentType();
			contentTypesField.addItem(item, value);
			if (contentType == current) {
				contentTypesField.setSelectedIndex(contentTypesField.getItemCount() - 1);
			}
		}
	}
	
	@UiHandler(value = "contentTypesField")
	void onContentTypeChange(ChangeEvent event) {
		String contentTypeStr = contentTypesField.getValue(contentTypesField.getSelectedIndex());
		ContentType contentType = ContentType.fromContentType(contentTypeStr);
		RecordClient.INSTANCE.setContentType(contentType);
	}
}
